package com.scs.application.core.pdf.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.annotation.JSONType;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.ParserConfig;
import com.scs.application.core.pdf.enums.PdfPageDirection;
import com.scs.application.core.pdf.enums.SpacingMode;
import com.scs.application.core.pdf.json.EnumDeserializer;
import com.scs.application.core.pdf.json.EnumSerializer;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.layout.property.TextAlignment;
import lombok.Data;
import lombok.experimental.Accessors;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description： PDF导出参数
 */
@Data
@Accessors(chain = true)
@JSONType(serializeEnumAsJavaBean = true, orders = {
        "project", "title", "subTitle", "barcodeText", "qrcodeText", "watermark", "watermarkFontSize",
        "pageDirection", "pageSize", "spacingMode",
        "headers", "gridColumns", "gridSpan",
        "perRowsOfGrid", "footers"
})
public class PdfExportParams implements Cloneable{

    private String project;
    private String title;
    private String subTitle;

    private String barcodeText;
    private String qrcodeText;

    @JSONField(serializeUsing = EnumSerializer.class, deserializeUsing = EnumDeserializer.class)
    private PdfPageDirection pageDirection = PdfPageDirection.LONGITUDINAL;

    @JSONField(serializeUsing = EnumSerializer.class, deserializeUsing = EnumDeserializer.class)
    private PageSize pageSize = PageSize.Default;

    @JSONField(serializeUsing = EnumSerializer.class, deserializeUsing = EnumDeserializer.class)
    private SpacingMode spacingMode = SpacingMode.NORMAL;

    private byte[] backgroundImageBytes;

    private List<PdfExportFormItem> headers;

    private List<PdfExportGridColumn> gridColumns;

    @JSONField(serialize = false)
    private List gridData;
    // 默认为24
    private int gridSpan = 24;


    /**
     * 网格列每页显示的行数
     */
    private Integer perRowsOfGrid = Integer.MAX_VALUE;

    private List<PdfExportFormItem> footers;

    private String watermark;
    private int watermarkFontSize = 32;


    public PdfExportParams setPageSize(PageSize pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    public PdfExportParams setPageSize(String pageSize) {
        try {
            Field field = PageSize.class.getField(pageSize.toUpperCase());
            this.pageSize = (PageSize) field.get(PageSize.class);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            this.setPageSize(PageSize.Default);
        }
        return this;
    }

    public PdfExportParams addHeaderItem(PdfExportFormItem item) {
        if (this.headers == null) {
            this.headers = new ArrayList<>();
        }
        this.headers.add(item);
        return this;
    }

    public PdfExportParams addHeaderItem(String label, Object value, int span) {
        if (this.headers == null) {
            this.headers = new ArrayList<>();
        }
        this.headers.add(new PdfExportFormItem("", label, value, span));
        return this;
    }

    public PdfExportParams addGridColumn(PdfExportGridColumn column) {
        if (this.gridColumns == null) {
            this.gridColumns = new ArrayList<>();
        }
        this.gridColumns.add(column);

        return this;
    }

    public PdfExportParams addGridColumn(String field, String label, int span) {
        if (this.gridColumns == null) {
            this.gridColumns = new ArrayList<>();
        }
        this.gridColumns.add(new PdfExportGridColumn(field, label, span));

        return this;
    }

    public PdfExportParams addFooterItem(PdfExportFormItem item) {
        if (this.footers == null) {
            this.footers = new ArrayList<>();
        }
        this.footers.add(item);

        return this;
    }

    public PdfExportParams addFooterItem(String label, Object value, int span) {
        if (this.footers == null) {
            this.footers = new ArrayList<>();
        }
        this.footers.add(new PdfExportFormItem("", label, value, span));
        return this;
    }

    public static PdfExportParams fromJson(String json) {
        ParserConfig parserConfig = new ParserConfig();
        parserConfig.putDeserializer(TextAlignment.class, new EnumDeserializer());
        return JSON.parseObject(json, PdfExportParams.class, parserConfig,
                Feature.IgnoreNotMatch, Feature.SupportArrayToBean);
    }
}
